package 泛型;

import java.util.Arrays;

class MyArray<T>{
    T[] array;
//    Object[] myArray = new Object[10];//最好的写法
//    T[] array = new Object[10];//错误
    public MyArray(int num) {
        array = (T[]) new Object[num];//向下转型
    }
    public void set(int pos, T val){
        array[pos] = val;
    }
    public  T get (int pos){
        return array[pos];
    }

    @Override
    public String toString() {
        return "MyArray{" +
                "array=" + Arrays.toString(array) +
                '}';
    }
}
public class text_1 {
    public static void main(String[] args) {
        MyArray<String> array = new MyArray<String>(10);
//      MyArray<int[]> array12 = new MyArray<>(12);
//      <>中只能是引用类型，不能是基本数据类型
        array.set(0,"hello");
        String a = array.get(0);
        System.out.println(a);

        MyArray<Integer> array1 = new MyArray<>(4);
        array1.set(0,1);
        array1.set(1,2);
        System.out.println(array1);

        //裸类型
        MyArray myArray = new MyArray(12);
    }
}
